/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
using System;
using System.IO;
using Madason.MetaModel;
using System.Collections.Generic;
using Madason.MetaModel.Interfaces;
using System.Xml;
using Madason.MetaModel.Validation;
using System.Reflection;
using Madason.GenUtils;

/// <summary>
/// Summary description for Program
/// </summary>
public class Program 
{




	/// <summary>
	/// Generate StrutsConfig
	/// </summary>
	/// <param name="sm"></param>
	/// <param name="configPath"></param>
	/// <param name="?"></param>
	public static void GenerateStrutsConfig(StateMachine sm, string configPath)
	{
		ProtectedSections sections = new ProtectedSections();
		FileInfo strutsFileInfo = new FileInfo(configPath);
		if (strutsFileInfo.Exists)
			sections.Load(strutsFileInfo);

		using (SensorWriter strutsConfigWriter = new SensorWriter(configPath))
		{			
			StrutsConfig strutsConfig = new StrutsConfig();
			strutsConfig.StateMachine = sm;
			strutsConfig.ProtectedSection = sections;
			strutsConfig.Render(strutsConfigWriter);
		}
	}


	public static StateMachine FindStrutsPageflow(Namespace ns)
	{
		foreach (StateMachine sm in ns.Statemachines)
		{
			Console.WriteLine("found sm: " + sm.Name + ", Stereotype is " + sm.StereotypeName);
			if (sm.StereotypeName == "StrutsPageflow")
				return sm;
		}

		foreach (Namespace childNs in ns.Namespaces)
		{
			StateMachine sm = FindStrutsPageflow(childNs);
			if (sm != null) return sm;
		}

		return null;
	}


	/// <summary>
	/// main function
	/// - parse command argumends
	/// - invoke special generator-functions
	/// </summary>
	/// <param name="args"></param>
	[STAThread]
	public static int Main(string[] args)
	{
		try
		{
			GeneratorWorkflow workflow = new GeneratorWorkflow(args);

			workflow.AddParameterDefinition("strutsConfig", ParameterType.File, "target path for struts-config xml file", true);
			workflow.CheckCommandLine();
			workflow.LoadModel();
			workflow.Validate();

			String configPath = workflow.GetArg("strutsConfig");

			StateMachine sm = FindStrutsPageflow(workflow.Model);
			Console.WriteLine("sm: " + sm);
			GenerateStrutsConfig(sm, configPath);
			return 0;
		}
		catch (Madason.GenUtils.ValidationException e)
		{
			Console.WriteLine(e.Message);
			return -1;
		}
		catch (Madason.GenUtils.MissingArgumentException e)
		{
			Console.WriteLine(e.Message);
			return -1;
		}
		
	}
}
